package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.MemberBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.PasswordInputView;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.ResponseBody;
import rx.Subscriber;


/**
 * Created by wei on 2017/12/22.
 * 根据众健号搜索会员/
 */

public class SplitStreamActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.member_et_sysid)
    EditText member_et_sysid;
    @BindView(R.id.number_ll_data_view)
    LinearLayout number_ll_data_view;
    @BindView(R.id.member_iv_heading_photo)
    CircleImageView member_iv_heading_photo;
    @BindView(R.id.member_tv_realname)
    TextView member_tv_realname;
    @BindView(R.id.member_tv_telphone)
    TextView member_tv_telphone;
    @BindView(R.id.member_tv_sysid)
    TextView member_tv_sysid;
    private int sendHeadId;
    private String passiveUserId;
    @BindView(R.id.member_tv_commit_pre)
    TextView member_tv_commit_pre;
    private String type;
    private Dialog hintDialog = null;


    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("我要分流");
        member_tv_commit_pre.setText("立即分流");
        type = getIntent().getStringExtra("type");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_presenter_num;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.member_tv_seach)
    public void onClickMemberSeach() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(member_et_sysid.getText().toString().trim())) {
            ToastUitl.showShort("众健号不能为空～");
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .getUrlRequestMemberBySysID(UserUtils.getUserToken(),
                        Integer.valueOf(member_et_sysid.getText().toString().trim()),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    MemberBean memberBean = gson.fromJson(responseBody.string(), MemberBean.class);
                                    sendHeadId = 0;
                                    if (memberBean.getError_code() == 0) {
                                        number_ll_data_view.setVisibility(View.VISIBLE);
                                        Picasso.get().load(BaseContacts.BASE_URL + memberBean.getData()
                                                .getHeadPhoto()).into(member_iv_heading_photo);
                                        member_tv_realname.setText(memberBean.getData().getTrueName());
                                        member_tv_telphone.setText(memberBean.getData().getUserName());
                                        member_tv_sysid.setText("众健号：" + memberBean.getData().getSysID());
                                        sendHeadId = memberBean.getData().getId();
                                    } else if (memberBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        number_ll_data_view.setVisibility(View.GONE);
                                        ToastUitl.showShort(memberBean.getError_message());
                                    }


                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });

    }

    /**
     * 立即分流
     */
    @OnClick(R.id.member_tv_commit_pre)
    public void onClickCommitPre() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(String.valueOf(sendHeadId)) || sendHeadId == 0) {
            ToastUitl.showShort("请先搜索众健号～");
            return;
        }
        hintDialog = new Dialog(SplitStreamActivity.this, R.style.dialog_style1);
        LinearLayout layout;
        LayoutInflater inflater = (LayoutInflater) SplitStreamActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = (LinearLayout) inflater.inflate(R.layout.dialog_hint,
                null);
        final PasswordInputView tv_hint = (PasswordInputView) layout
                .findViewById(R.id.again_paypswd_pet);
        layout.findViewById(R.id.dialog_btn_cancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        hintDialog.dismiss();
                    }
                });
        layout.findViewById(R.id.dialog_btn_ensure).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(tv_hint.getText().toString().trim())) {
                            ToastUitl.showShort("输入密码不能为空");
                            return;

                        }
                        initRequestPayPassword(tv_hint.getText().toString().trim());
                        hintDialog.dismiss();
                    }
                });
        hintDialog.setContentView(layout);
        hintDialog.show();

    }

    private void initRequestPayPassword(String password) {
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestPayPassword(UserUtils.getUserToken(),
                        password, new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        initViewRequest();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                            }
                        });

    }


    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;

        } else if (TextUtils.isEmpty(String.valueOf(sendHeadId)) || sendHeadId == 0) {
            ToastUitl.showShort("请先搜索众健号～");
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestSplitStream(UserUtils.getUserToken(),
                        Integer.valueOf(sendHeadId),
                        Integer.valueOf(type), new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort(baseBean.getError_message());
                                        finish();
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });
    }
}
